package com.ty.thymeleaf.controller;

import com.ty.thymeleaf.entity.User;
import com.ty.thymeleaf.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UserController
 *
 * @aurhor Administrator  whs
 * @since 2024/9/4
 */
@RestController
public class UserController {

    @Resource
    private UserService userService;  // 根据id 查询数据
    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public User getUser(Long usrId){
        User user = userService.getUser(usrId);
        return user;
    }


    @GetMapping(value = "/users")  // 查询所有数据
    public List<User> findAllUsers(){
        return userService.findAllUsers();
    }

    @GetMapping(value = "/user/{id}") // 根据 @PathVariable 注入id 查询数据
    public User getUsers(@PathVariable("id") Long usrId){
        return userService.getUser(usrId);
    }

    @PostMapping(value = "/userAdd")  // 添加
    public void addUser(User user){



        user.setUsrId(null);
        user.setUsrName("帅帅");
        user.setUsrPassword("1234321");
        user.setUsrRoleId(2L);
        user.setUsrFlag(1L);
        System.out.println(userService.addUser(user)==1?"\n\n\n添加成功！":"\n\n\n添加失败！");
    }

    @PostMapping(value = "/user/update") // 修改
    public void updateUser(User user){
        System.out.println(userService.updateUser(user)==1?"添加成功！":"添加失败！");
    }

//    @DeleteMapping(value = "/user/del/{id}") // 删除
//    public void deleteUser(@PathVariable("id") Long usrId){
//        userService.deleteUser(usrId);
//    }

    @PostMapping(value = "/user/del/{id}")  // 添加
    public void delUser(@PathVariable("id") Long usrId){
        userService.deleteUser(usrId);
    }



}
